package com.yang.net.tcp;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author yds
 * @program net-sokect_stduy
 * @description 作为tcp聊天室的客户端
 * @create 2020-09-27 16:01
 **/
public class TcpClient {
    //知道服务器地址
    public static void main(String[] args) {
        Socket socket=null;
        OutputStream out=null;
        try {
            //1. 提供一个InetAdress地址 ip+端口
            InetAddress toAddress = InetAddress.getByName("127.0.0.1");
            int port=9999;
            //2. 创建一个socket连接
           socket = new Socket(toAddress,port);
            //3. 发送一个io流消息
           out = socket.getOutputStream();
           out.write("hello 服务器！".getBytes());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }finally {
            try {
                if(socket!=null){
                    socket.close();
                }
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}